# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time
from utils.operators.rich_sql_sensor import RichSqlSensor
from jms.dm.dm_tab_scan_piece_detail_dt import jms_dm__dm_tab_scan_piece_detail_dt

label = f"dm_tab_scan_send_piece_detail_dt_{int(time())}"
timeout = timedelta(minutes=20).seconds
doris_jms_dm__dm_tab_scan_send_piece_detail_dt = RichSqlSensor(
    task_id='doris_jms_dm__dm_tab_scan_send_piece_detail_dt',
    pool='broker_load_pool',
    depends_on_past=True,
    email=['jokic.wang@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    conn_id='doris',
    pre_sql=f""" 
                  TRUNCATE TABLE jms_dm.dm_tab_scan_send_piece_detail_dt PARTITION ( 
                  p{{{{ execution_date | cst_ds_nodash }}}} 
                 ,p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}} 
                 ); 
                 LOAD LABEL jms_dm.{label} (
                    DATA INFILE(
                     "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_tab_scan_piece_detail_dt/dt={{{{  execution_date | cst_ds  }}}}/scan_data_type=1/*",
                     "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_tab_scan_piece_detail_dt/dt={{{{  execution_date | date_add(-1) | cst_ds  }}}}/scan_data_type=1/*"
                     ) INTO TABLE dm_tab_scan_send_piece_detail_dt
                      FORMAT AS 'PARQUET' (
                                           hours              ,
                                           format_scan_time   ,
                                           package_code       ,
                                           scan_user_code     ,
                                           send_site_code     ,
                                           network_code       ,
                                           waybill_no         ,
                                           organization_code  ,
                                           organization_name  ,
                                           scan_type          ,
                                           scan_time          ,
                                           network_name       ,
                                           network_type       ,
                                           pre_site_code      ,
                                           pre_site_name      ,
                                           next_site_code     ,
                                           next_site_name     ,
                                           shipment_no        ,
                                           shipment_name      ,
                                           scan_next_pre_code ,
                                           scan_next_pre_name ,
                                           scan_user          ,
                                           scan_user_id       ,
                                           scan_user_type     ,
                                           send_site          ,
                                           pcs                ,
                                           weight             ,
                                           upload_time        ,
                                           current_send_package_code ,
                                           current_send_scan_time ,
                                           supplier_id        ,
                                           supplier_code      ,
                                           supplier_name      ,
                                           goods_type_id,
                                           goods_type_code,
                                           goods_type_name,
                                           is_parts,
                                           is_scans,
                                           date_time,
                                           sender_city_id,
                                           sender_city_name,
                                           receiver_city_id,
                                           receiver_city_name,
                                           receiver_network_code,
                                           receiver_network_name,
                                           table_type,
                                           bz_type
                      )
                      columns from path as (dt)
                      set (
                                           hours              =  hours              ,
                                           format_scan_time   =  format_scan_time   ,
                                           package_code       =  package_code       ,
                                           scan_user_code     =  scan_user_code     ,
                                           send_site_code     =  send_site_code     ,
                                           network_code       =  network_code       ,
                                           waybill_no         =  waybill_no         ,
                                           organization_code  =  organization_code  ,
                                           organization_name  =  organization_name  ,
                                           scan_type          =  scan_type          ,
                                           scan_time          =  scan_time          ,
                                           network_name       =  network_name       ,
                                           network_type       =  network_type       ,
                                           pre_site_code      =  pre_site_code      ,
                                           pre_site_name      =  pre_site_name      ,
                                           next_site_code     =  next_site_code     ,
                                           next_site_name     =  next_site_name     ,
                                           shipment_no        =  shipment_no        ,
                                           shipment_name      =  shipment_name      ,
                                           scan_next_pre_code =  scan_next_pre_code ,
                                           scan_next_pre_name =  scan_next_pre_name ,
                                           scan_user          =  scan_user          ,
                                           scan_user_id       =  scan_user_id       ,
                                           scan_user_type     =  scan_user_type     ,
                                           send_site          =  send_site          ,
                                           pcs                =  pcs                ,
                                           weight             =  weight             ,
                                           upload_time        =  upload_time        ,
                                           current_send_package_code = current_send_package_code,
                                           current_send_scan_time = current_send_scan_time ,
                                           supplier_id        = supplier_id,
                                           supplier_code      = supplier_code,
                                           supplier_name      = supplier_name,
                                           goods_type_id      = goods_type_id,
                                           goods_type_code    = goods_type_code,
                                           goods_type_name    = goods_type_name,
                                           is_parts           = is_parts,
                                           is_scans           = is_scans,
                                           date_time          =  date_time,
                                           sender_city_id         = sender_city_id,
                                           sender_city_name       = sender_city_name,
                                           receiver_city_id       = receiver_city_id,
                                           receiver_city_name     = receiver_city_name,
                                           receiver_network_code  = receiver_network_code,
                                           receiver_network_name  = receiver_network_name,
                                           table_type             = table_type,
                                           bz_type                = bz_type
                      )
                 )
                 WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
                 PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')
                 """,
    poke_sql=f"SHOW LOAD FROM jms_dm WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dm WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dm__dm_tab_scan_send_piece_detail_dt << jms_dm__dm_tab_scan_piece_detail_dt
